package com.qianfeng.smartplatform.controller;/*
 *@Description:
 *@author:赵健
 *@Date:zjzzz 2022/9/7 17:55
 */

import com.qianfeng.smartplatform.dto.R;
import com.qianfeng.smartplatform.dto.UserDto;
import com.qianfeng.smartplatform.pojo.SysUserInfo;
import com.qianfeng.smartplatform.service.UserService;
import com.qianfeng.smartplatform.valigroup.Insert;
import com.qianfeng.smartplatform.valigroup.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/user")
    public R register(@Validated(Insert.class) @RequestBody UserDto userDto) {
        System.err.println(userDto);
        userService.addUser(userDto);
        return R.OK();
    }

    @PostMapping("/login")
    public R login(@RequestBody @Validated(Select.class) UserDto userDto, HttpSession session) {
        SysUserInfo sysUserInfo = userService.login(userDto.getUsername(), userDto.getPassword());
        session.setAttribute("user", sysUserInfo);
        return R.OK();
    }
}
